<template>
  <el-dialog
    :title="!dataForm.agentId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="经纪人头像" prop="agentImg">
      <el-input v-model="dataForm.agentImg" placeholder="经纪人头像"></el-input>
    </el-form-item>
    <el-form-item label="经纪人姓名" prop="agentName">
      <el-input v-model="dataForm.agentName" placeholder="经纪人姓名"></el-input>
    </el-form-item>
    <el-form-item label="经纪人手机号" prop="agentPhone">
      <el-input v-model="dataForm.agentPhone" placeholder="经纪人手机号"></el-input>
    </el-form-item>
    <el-form-item label="经纪人性别" prop="agentSex">
      <el-input v-model="dataForm.agentSex" placeholder="经纪人性别"></el-input>
    </el-form-item>
    <el-form-item label="经纪人所在区域" prop="agentAid">
      <el-input v-model="dataForm.agentAid" placeholder="经纪人所在区域"></el-input>
    </el-form-item>
    <el-form-item label="经纪人详细地址" prop="agentAddress">
      <el-input v-model="dataForm.agentAddress" placeholder="经纪人详细地址"></el-input>
    </el-form-item>
    <el-form-item label="经纪人公司" prop="companyId">
      <el-input v-model="dataForm.companyId" placeholder="经纪人公司"></el-input>
    </el-form-item>
    <el-form-item label="新房个数" prop="newHouseNum">
      <el-input v-model="dataForm.newHouseNum" placeholder="新房个数"></el-input>
    </el-form-item>
    <el-form-item label="二手房个数" prop="secondHouseNum">
      <el-input v-model="dataForm.secondHouseNum" placeholder="二手房个数"></el-input>
    </el-form-item>
    <el-form-item label="租房个数" prop="rentHouseNum">
      <el-input v-model="dataForm.rentHouseNum" placeholder="租房个数"></el-input>
    </el-form-item>
    <el-form-item label="经纪人状态
0:在职
1:离职
2:逻辑删除隐藏" prop="agentStatus">
      <el-input v-model="dataForm.agentStatus" placeholder="经纪人状态
0:在职
1:离职
2:逻辑删除隐藏"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          agentId: 0,
          agentImg: '',
          agentName: '',
          agentPhone: '',
          agentSex: '',
          agentAid: '',
          agentAddress: '',
          companyId: '',
          newHouseNum: '',
          secondHouseNum: '',
          rentHouseNum: '',
          agentStatus: ''
        },
        dataRule: {
          agentImg: [
            { required: true, message: '经纪人头像不能为空', trigger: 'blur' }
          ],
          agentName: [
            { required: true, message: '经纪人姓名不能为空', trigger: 'blur' }
          ],
          agentPhone: [
            { required: true, message: '经纪人手机号不能为空', trigger: 'blur' }
          ],
          agentSex: [
            { required: true, message: '经纪人性别不能为空', trigger: 'blur' }
          ],
          agentAid: [
            { required: true, message: '经纪人所在区域不能为空', trigger: 'blur' }
          ],
          agentAddress: [
            { required: true, message: '经纪人详细地址不能为空', trigger: 'blur' }
          ],
          companyId: [
            { required: true, message: '经纪人公司不能为空', trigger: 'blur' }
          ],
          newHouseNum: [
            { required: true, message: '新房个数不能为空', trigger: 'blur' }
          ],
          secondHouseNum: [
            { required: true, message: '二手房个数不能为空', trigger: 'blur' }
          ],
          rentHouseNum: [
            { required: true, message: '租房个数不能为空', trigger: 'blur' }
          ],
          agentStatus: [
            { required: true, message: '经纪人状态
0:在职
1:离职
2:逻辑删除隐藏不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.agentId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.agentId) {
            this.$http({
              url: this.$http.adornUrl(`/agent/agent/info/${this.dataForm.agentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.agentImg = data.agent.agentImg
                this.dataForm.agentName = data.agent.agentName
                this.dataForm.agentPhone = data.agent.agentPhone
                this.dataForm.agentSex = data.agent.agentSex
                this.dataForm.agentAid = data.agent.agentAid
                this.dataForm.agentAddress = data.agent.agentAddress
                this.dataForm.companyId = data.agent.companyId
                this.dataForm.newHouseNum = data.agent.newHouseNum
                this.dataForm.secondHouseNum = data.agent.secondHouseNum
                this.dataForm.rentHouseNum = data.agent.rentHouseNum
                this.dataForm.agentStatus = data.agent.agentStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/agent/agent/${!this.dataForm.agentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'agentId': this.dataForm.agentId || undefined,
                'agentImg': this.dataForm.agentImg,
                'agentName': this.dataForm.agentName,
                'agentPhone': this.dataForm.agentPhone,
                'agentSex': this.dataForm.agentSex,
                'agentAid': this.dataForm.agentAid,
                'agentAddress': this.dataForm.agentAddress,
                'companyId': this.dataForm.companyId,
                'newHouseNum': this.dataForm.newHouseNum,
                'secondHouseNum': this.dataForm.secondHouseNum,
                'rentHouseNum': this.dataForm.rentHouseNum,
                'agentStatus': this.dataForm.agentStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
